package com.cdgc.web.controller;

import com.cdgc.web.pojo.Sp;
import com.cdgc.web.service.SpService;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class SpController {
    @Autowired
    private SpService spService;

    @RequestMapping("/queryAllSps")
    public ModelAndView queryAllSps(){
        List<Sp> spList = spService.queryAllSps();
        ModelAndView mv = new ModelAndView();
        mv.addObject("spList",spList);
        mv.setViewName("sptable");
        return mv;
    }

    @RequestMapping("/deleteSpById")
    private String deleteSpById(Integer id){
        spService.deleteSpById(id);
        return "redirect:/queryAllSps";
    }

    @RequestMapping("/toUpdateSpById")
    private ModelAndView toUpdateSpById(Integer id){
        Sp sp = spService.querySpsById(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("sp",sp);
        mv.setViewName("UpdateSp");
        return mv;
    }

    @RequestMapping("/UpdateSp")
    public String UpdateSp(Sp sp){
        spService.UpdateSp(sp);
        return "redirect:/queryAllSps";
    }

    @RequestMapping("/addNewSp")
    public String addNewSp(Sp sp){
        spService.addNewSp(sp);
        return "redirect:/queryAllSps";
    }
}
